#include "resourcemodel.h"
#include "resourcemanager.h"
#include "../../graphic/allochierarchy.h"
#include "../defines.h"

namespace gunsmith
{
CResourceModel::CResourceModel() : CResource(RT_MODEL)
{
	m_pAnimCtrl		= NULL;
	m_pFrameRoot	= NULL;
}

CResourceModel::~CResourceModel()
{
}

/*!	\brief		Creates a model hierarchy from a given file name.
	\param		pSource	Takes a null terminated string as source.
	\return		bool	Returns true on successful creation.
	\remarks	
	\sa			*/
bool CResourceModel::Create(void *pSource)
{
	HRESULT hr;
	CAllocHierarchy allocHierarchy;
	allocHierarchy.m_pDevice = m_pDevice;

	const char *cpSource = (const char*)pSource;

	//! \todo consider writing a little file manager that can search for files and does some error handling
	hr = D3DXLoadMeshHierarchyFromX(cpSource, D3DXMESH_MANAGED, m_pDevice, &allocHierarchy, NULL, &m_pFrameRoot, &m_pAnimCtrl);

	return SUCCEEDED(hr);
}

/*!	\brief		Destroys the model and frame hierarchy and releases the animation controller.
	\return		void
	\remarks	
	\sa			*/
void CResourceModel::Destroy()
{
	CAllocHierarchy allocHierarchy;
	D3DXFrameDestroy(m_pFrameRoot, &allocHierarchy);

	RELEASE(m_pAnimCtrl);
}

/*!	\brief		Returns the animation controller to the model.
	\return		ID3DXAnimationController*	Animation controller.
	\remarks	
	\sa			*/
ID3DXAnimationController* CResourceModel::GetAnimControl() const
{
	return m_pAnimCtrl;
}

/*!	\brief		Returns root frame.
	\return		D3DXFRAME*	Root frame.
	\remarks	
	\sa			*/
D3DXFRAME* CResourceModel::GetFrameRoot() const
{
	return m_pFrameRoot;
}
};